import Mock from 'mockjs'

// 获取短信验证码
Mock.mock(/\/api\/user\/sms/, 'post', (options) => {
  const { phone } = JSON.parse(options.body)
  
  if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
    return {
      code: 400,
      message: '手机号格式不正确'
    }
  }
  
  return {
    code: 200,
    message: '验证码发送成功',
    data: {
      phone,
      expire: 300 // 验证码有效期（秒）
    }
  }
})

// 手机号验证码登录
Mock.mock(/\/api\/user\/login\/sms/, 'post', (options) => {
  const { phone, code } = JSON.parse(options.body)
  
  if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
    return {
      code: 400,
      message: '手机号格式不正确'
    }
  }
  
  if (!code || code !== '123456') {
    return {
      code: 400,
      message: '验证码错误'
    }
  }
  
  return {
    code: 200,
    message: '登录成功',
    data: {
      token: 'mock_token_' + Date.now(),
      expire: 7 * 24 * 60 * 60 // token有效期（秒）
    }
  }
})

// 获取用户信息
Mock.mock(/\/api\/user\/info/, 'get', () => {
  return {
    code: 200,
    message: '获取用户信息成功',
    data: {
      userId: '1',
      username: '测试用户',
      avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
      phone: '13800138000',
      roles: ['admin'],
      createTime: Mock.Random.datetime()
    }
  }
})

// 退出登录
Mock.mock(/\/api\/user\/logout/, 'post', () => {
  return {
    code: 200,
    message: '退出登录成功'
  }
}) 